#!/usr/bin/env python
#-*- coding:utf-8 -*-

'''
Created on 2012-01-15

@author: zhangchong
'''


#/data/log/log/[0-9,a-z]*/[0-9]*[0-9]

import sys
import os
import json
import string
import MySQLdb
import simplejson
import Config
import datetime

import CpLog
import Split
import Statist



import accuTotal
import accuUserPerDay
import newUserPerDay

import traceback

from Statist import *





class Access(Static):
    save = "access.json"
    log = Static.accessLog
    urlReg = [
            (r'wireless/getRelated', "" , "related", "", "success", "", ""),
            (r'wireless/getVideoDetail', "" , "video_detail", "", "success", "", ""),
            (r'cms/ipad/getShowRecList', "" , "show_reclist", "", "success", "", ""),
            (r'cms/ipad/getShowTopList', "" , "show_toplist", "", "success", "", ""),
            (r'wireless/user/login', "" , "login", "", "success", "", ""),
            (r'wireless/user/favorites/add', "" , "fav_add", "", "success", "", ""),
            (r'wireless/user/favorites/del', "" , "fav_del", "", "success", "", ""),
                     
            (r'/phone2/main', "" , "phone_main", "", "success", "", ""),
            (r'/phone2_1/detail', "" , "phone_detail", "", "success", "", ""),
            (r'/ipad2_2/main', "" , "ipad_main", "", "success", "", ""),
            (r'/ipad2_2/desc', "" , "ipad_desc", "", "success", "", ""),
            (r'/playurl', "" , "playurl", "", "success", "", ""),
            (r'/search/', "" , "search", "", "success", "", ""),
            (r'/user/register', "" , "register", "register", "code200", "", ""),
            (r'/user/uploads/add', "" , "upload", "upload", "code200", "", "")
    ]
  

class All(Static):
    save = "all.json"
    log = Static.accessLog
    urlReg = [
            (r"all", "" , "all", "pv", "total", "uv", "total"),
    ]
  
    
class VV_Begin(Static):
    save = "vv_begin.json";
    log = Static.statisLog
    urlReg = [
#            (r"/statis/vv\.?(json|xml|text)?", "type=begin" , "vv", "vv", "total", "", "")
            (r"/statis/vv", "type=begin" , "vv_begin", "vv", "total", "", "")
    ]
    
class VV_End(Static):
    save = "vv_end.json";
    log = Static.statisLog
    urlReg = [
            (r"/statis/vv", "type=end" , "vv_end", "", "total", "", "")
    ]
    
class VV_NoBeginEnd(Static):
    save = "vv_nobeginend.json";
    log = Static.statisLog
    urlReg = [
            (r"/statis/vv", "!type=begin type=end" , "vv_nobegin_noend", "", "total", "", ""),
    ]        

class VV(Static):
    save = "vv.json";
    log = Static.statisLog
    urlReg = [
            (r"/statis/vv", "" , "vv", "", "total", "", "")
    ]   
   
class Initial(Static):
    save = "initial.json";
    log = Static.statisLog
    urlReg = [
          (r"/initial", "" , "initial", "", "code200", "", "")    
          ]

class InitialGuid(Static):
    save = "initial_guid.json";
    log = Static.statisLog
    urlReg = [
          (r"/initial", "guid=[a-z0-9]{32}" , "initial", "", "code200", "", "")    
          ]
        
class InitialNoGuid(Static):
    save = "initial_noguid.json";
    log = Static.statisLog
    urlReg = [
          (r"/initial", "!guid=[a-z0-9]{32}" , "initial", "", "code200", "", "")    
          ]    


if __name__ == "__main__":
   
   
    argv = sys.argv
    statdate = argv[1]
    
    isSplit = True
    try:
        argv2 = argv[2]
        if(str(argv2) == "sort"):
            isSplit = False
    except:
        isSplit = True 
    
    
    
    vv_Begin = VV_Begin(statdate)
    vv_End = VV_End(statdate)
    vv_noBeginEnd = VV_NoBeginEnd(statdate)
    vv = VV(statdate)

    access = Access(statdate)
    all = All(statdate)
   
    initial = Initial(statdate)
    initialGuid = InitialGuid(statdate)
    initialNoGuid = InitialNoGuid(statdate)
   
    
    delTmpLog()
    
    resultJson = statist(vv_Begin, format="json", isSplit=isSplit) 
#    print json.dumps(resultJson, ensure_ascii=False)
#    statist(vv_End, isSplit=isSplit)
#    statist(vv_noBeginEnd, isSplit=isSplit)
#    statist(vv, isSplit=isSplit)
#   
#    statist(access, isSplit=isSplit)
#    statist(all, isSplit=isSplit)
#
#    statist(initial, isSplit=isSplit)
#    statist(initialGuid, isSplit=isSplit)
#    statist(initialNoGuid, isSplit=isSplit)
    
    
    delTmpLog()
    


    print "-----statist save DB-----"   
    starttime = time.time()
    try: 
        insert(vv_Begin, "all")
    except:
        traceback.print_exc()
        
#    try:
#        insert(access, "all")
#    except:
#        traceback.print_exc()
#        
#    try:
#        insert(all, "all")
#    except:
#        traceback.print_exc()
        
    endtime = time.time()
    print "-----statist save DB Run times:" + str(int((endtime - starttime))) + "-----\n"
    


    
   
